package app.mvc.view.components.panels
{
	import alternativa.gui.event.DragEvent;
	import alternativa.gui.theme.defaulttheme.container.panel.EditorPanel;
	import alternativa.gui.theme.defaulttheme.container.panel.Panel;
	import alternativa.gui.theme.defaulttheme.event.PanelEvent;
	
	import flash.events.Event;
	
	import globals.Global;
	
	import utils.Tracker;
	
	/**
	 * 编辑器面板基类
	 * @author xiling
	 */
	public class BasePanel extends EditorPanel
	{
		/**
		 * 拖动矩形范围
		 * @default 
		 */
		protected var minDragX:int = 0;
		protected var minDragY:int = 0;
		protected var maxDragX:int = 0;
		protected var maxDragY:int = 0;
		
		public function BasePanel()
		{
			super();
			dragEnabled=true;
			initListeners();
		}
		
		protected function initListeners():void
		{
			Global.stage.addEventListener(Event.RESIZE,onResize);
			addEventListener(PanelEvent.MOUSE_DOWN,onPanelMouseDown);
			addEventListener(PanelEvent.MOUSE_UP,onPanelMouseUp);
		}
		
		/**
		 * 为了拖动而监听
		 * @param evt
		 */
		private function onPanelMouseDown(evt:PanelEvent):void
		{
			if(dragEnabled)
			{
				addEventListener(Event.ENTER_FRAME,onEnterFrame);
			}
		}
		
		private function onPanelMouseUp(evt:PanelEvent):void
		{
			removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			onEnterFrame(null);
		}
		
		/**
		 * 拖动中检查限制
		 * @param evt
		 */
		private function onEnterFrame(evt:Event):void
		{
			if(x<minDragX)
				x = minDragX;
			if(y<minDragY)
				y = minDragY;
			if(x>maxDragX)
				x = maxDragX;
			if(y>maxDragY)
				y = maxDragY;
		}
		
		protected function onResize(evt:Event):void
		{
			// TODO Auto Generated method stub
			super.draw();
			
			
			if(parent)
			{
				maxDragX = parent.width-this.width;
				maxDragY = parent.height-this.height;
			}
			
			onEnterFrame(null);//计算RESIZE后,XY的限制
			
			//Tracker.say(Global.stageW,this.width);
		}
	}
}